package cn.fxl.system.service.impl;

import cn.fxl.basic.service.impl.BasicServiceImpl;
import cn.fxl.system.domain.Role;
import cn.fxl.system.mapper.RoleMapper;
import cn.fxl.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BasicServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    @Transactional
    public void save(Role role) throws MessagingException {
        super.save(role);
        saveRoleMenuAndPermission(role);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        roleMapper.removeMenuRoleId(id);
        roleMapper.removePermissionRoleId(id);
        roleMapper.removeEmployeeRoleId(id);
        roleMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);
        roleMapper.removeMenuRoleId(role.getId());
        roleMapper.removePermissionRoleId(role.getId());
        saveRoleMenuAndPermission(role);
    }

    public  void saveRoleMenuAndPermission(Role role){
        List<Long> menus = role.getMenus();
        if(menus.size()!=0){
            roleMapper.saveRoleMenu(role);
        }
        List<Long> permissions = role.getPermissions();
        if(permissions.size()>0){//当前选择寻找了菜单
            roleMapper.savePermission(role);
        }
    }
}
